package com.caigou.controller.front;

import com.caigou.bean.cms.entity.Article;
import com.caigou.bean.cms.entity.ArticleStatistics;
import com.caigou.bean.cms.entity.EsArticle;
import com.caigou.bean.cms.param.EsArticleParam;
import com.caigou.bean.cms.param.FontArticlePram;
import com.caigou.pojo.ArticleList;
import com.caigou.service.ArticleService;
import com.caigou.service.IEsArticleService;
import com.caigou.util.CookieUtils;
import com.caigou.util.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
//@CrossOrigin
@RestController
@RequestMapping("/front/article")
public class FontArticleController {

    @Resource
    private ArticleService articleService;
    @Resource
    private IEsArticleService esArticleService;

    @RequestMapping("/loadHotSearchArticle")
    public Result loadHotSearchArticle(@RequestBody FontArticlePram pram){
        return articleService.loadHotSearchArticle(pram);
    }

    @ApiOperation("专题列表查询(全部频道)")
    @RequestMapping("/loadSpecialArticleList")
    public Result loadSpecialArticleList(@RequestBody FontArticlePram pram){
        return articleService.loadSpecialArticleList(pram);
    }

    @ApiOperation("专题列表查询(按频道id查询文章列表)")
    @PostMapping("/list")
    public Result<PageInfo<ArticleList>> findArticleList(@RequestBody FontArticlePram pram){
        return articleService.findArticleListByPlateId(pram);
    }

    @ApiOperation("文章点击量统计接口")
    @PostMapping("/saveArticleClickCount")
    //@ResponseBody
    public Result saveArticleClickCount(@RequestBody FontArticlePram pram, HttpServletRequest request, HttpServletResponse response){

        // 移动端点击量统计判定
        if (null != pram.getIsMobile() && pram.getIsMobile() == 1){
            log.info("增加移动端点击量:"+pram.getArticleId());
            Article article1 = articleService.loadArticle(pram.getArticleId());
            log.info(pram.getArticleId()+"增加移动点击量之前的值为："+article1.getArticleClickPhone());
            int result = articleService.saveArticleClickPhone(pram.getArticleId());
            log.info(pram.getArticleId()+"移动端点击量增加结果："+result);
            Article article2 = articleService.loadArticle(pram.getArticleId());
            log.info(pram.getArticleId()+"增加移动点击量之后的值为："+article2.getArticleClickPhone());
        } else {
            // PC点击量统计判定
            String str = CookieUtils.getCookie(request,"lrzdcecookieinforecordpc");
            if (str == null){
                log.info("没有获取到cookies数据，进行cookies数据填充,增加PC端点击量:"+pram.getArticleId());
                CookieUtils.writeCookie(request,response,"lrzdcecookieinforecordpc", pram.getArticleId());

                Article article1 = articleService.loadArticle(pram.getArticleId());
                log.info(pram.getArticleId()+"增加PC点击量之前的值为："+article1.getArticleClick());
                int result = articleService.saveArticleClick(pram.getArticleId());
                log.info(pram.getArticleId()+"PC点击量增加结果："+result);
                Article article2 = articleService.loadArticle(pram.getArticleId());
                log.info(pram.getArticleId()+"增加PC点击量之后的值为："+article2.getArticleClick());

            } else {
                log.info("有缓存数据，进行缓存验证");
                boolean isExits = false;
                String[] cookieStr = str.split("%2C");
                for (String info: cookieStr){
                    if (null != info && !info.equals("")){
                        if (info.equals(pram.getArticleId())){
                            isExits = true;
                            break;
                        }
                    }
                }
                if (!isExits){
                    log.info("未通过cookie验证，可以增加点击量,增加PC端点击量:"+pram.getArticleId());
                    CookieUtils.writeCookie(request,response,"lrzdcecookieinforecordpc", pram.getArticleId());

                    Article article1 = articleService.loadArticle(pram.getArticleId());
                    log.info(pram.getArticleId()+"增加PC点击量之前的值为："+article1.getArticleClick());
                    int result = articleService.saveArticleClick(pram.getArticleId());
                    log.info(pram.getArticleId()+"PC点击量增加结果："+result);
                    Article article2 = articleService.loadArticle(pram.getArticleId());
                    log.info(pram.getArticleId()+"增加PC点击量之后的值为："+article2.getArticleClick());

                } else {
                    log.info("已通过cookie验证，不增加点击量");
                }
            }
        }
        //服务器server端要配置Access-Control-Allow-Credentials
        response.setHeader("Access-Control-Allow-Credentials", "true");
        //提示我们 Access-Control-Allow-Origin 不能用 * 通配符
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        return articleService.saveArticleClickCount(pram);
    }

    @ApiOperation("文章点击量统计查询接口")
    @PostMapping("/statistics/list")
    public Result<PageInfo<ArticleStatistics>> getArticleStatistics(@RequestBody FontArticlePram pram) {
        return articleService.findArticleStatisticsByParam(pram);
    }

    @ApiOperation("前台查询图集库列表接口")
    @PostMapping("/atlas/list")
    public Result<PageInfo<ArticleList>> getArticleAtlas(@RequestBody FontArticlePram pram) {
        return articleService.findArticleAtlasByParam(pram);
    }

    @ApiOperation("前台查询音频(亚利聊政采)列表接口")
    @PostMapping("/audio/list")
    public Result<PageInfo<ArticleList>> getArticleAudios(@RequestBody FontArticlePram pram) {
        return articleService.findArticleAudioByParam(pram);
    }

    @PostMapping("/es/list")
    @ApiOperation("ES搜索文章")
    public Result<PageInfo<ArticleList>> getEsArticleList(@RequestBody EsArticleParam pram) {
        return esArticleService.findEsArticleListByParam(pram);
    }

    @ApiOperation("移动分享端文章详情页接口")
    @RequestMapping("/mobile/loadArticleInfo")
    public Result loadArticleMobileInfo(@RequestBody FontArticlePram pram){
        return articleService.loadArticleMobileInfo(pram);
    }
}
